#!/bin/bash


echo "=================================================="
date +"【%Y-%m-%d %H:%M:%S】开始关闭服务"

# 获取服务进程ID
APP_PID=$(ps -ef | grep "com.siasun.vaemrd.VaemrdApplication" | grep -v grep | awk '{print $2}')

if [ -z "$APP_PID" ]; then
    echo "Service is not running"
    exit 0
fi

echo "Stopping service (PID: $APP_PID)..."
kill $APP_PID

# 等待最多10秒
TIMEOUT=10
while [ $TIMEOUT -gt 0 ]; do
    if ! ps -p $APP_PID > /dev/null; then
        echo "Service stopped successfully"
        exit 0
    fi
    sleep 1
    ((TIMEOUT--))
done

# 强制终止（如果正常终止失败）
echo "Service did not stop gracefully, force killing..."
kill -9 $APP_PID
sleep 1
if ! ps -p $APP_PID > /dev/null; then
    echo "Service force stopped"
else
    echo "Failed to stop service"
    exit 1
fi

